var _excluded = [
  'treeData',
  'className',
  'value',
  'onChange',
  'fieldNames',
  'loadNodeDetail',
  'searchable',
  'loadTreeDataBySearch',
];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { debounce } from '@szzj/utils';
import { TreeSelect as AntdTreeSelect } from 'antd';
import classnames from 'classnames';
import React, { forwardRef, useCallback } from 'react';
import createField from '../createField';
import useLazyTreeSelect from './hooks/useLazyTreeSelect';
/**
 * 树选择框
 */
var LazyTreeSelect = /*#__PURE__*/ forwardRef(function (_ref, ref) {
  var loadTreeData = _ref.treeData,
    className = _ref.className,
    value = _ref.value,
    onChange = _ref.onChange,
    fieldNames = _ref.fieldNames,
    loadNodeDetail = _ref.loadNodeDetail,
    searchable = _ref.searchable,
    loadTreeDataBySearch = _ref.loadTreeDataBySearch,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useLazyTreeSelect = useLazyTreeSelect({
      value: value,
      loadTreeData: loadTreeData,
      fieldNames: fieldNames,
      loadNodeDetail: loadNodeDetail,
      loadTreeDataBySearch: loadTreeDataBySearch,
    }),
    treeNodesBySearch = _useLazyTreeSelect.treeNodesBySearch,
    searchExpandedKeys = _useLazyTreeSelect.searchExpandedKeys,
    label = _useLazyTreeSelect.label,
    treeNodes = _useLazyTreeSelect.treeNodes,
    loadDataByPatentCode = _useLazyTreeSelect.loadDataByPatentCode,
    setExpandedKeys = _useLazyTreeSelect.setExpandedKeys,
    expandedKeys = _useLazyTreeSelect.expandedKeys,
    handleSearch = _useLazyTreeSelect.handleSearch,
    setTreeNodesBySearch = _useLazyTreeSelect.setTreeNodesBySearch;
  var searchProps = searchable
    ? {
        showSearch: true,
        filterTreeNode: function filterTreeNode() {
          return true;
        },
        onSearch: debounce(handleSearch, 1000),
        onBlur: function onBlur() {
          return setTreeNodesBySearch([]);
        },
      }
    : {};
  var handleChange = useCallback(
    function (val, labelList, extra) {
      // @ts-ignore
      onChange === null || onChange === void 0 ? void 0 : onChange(val, labelList, extra);
    },
    [onChange],
  );
  return /*#__PURE__*/ React.createElement(
    AntdTreeSelect,
    _extends(
      {
        placeholder: '\u8BF7\u9009\u62E9'.concat(label),
        className: classnames('szzj-tree-select', className),
        ref: ref,
        treeData: treeNodesBySearch.length > 0 ? treeNodesBySearch : treeNodes,
        loadData: loadDataByPatentCode,
        value: value,
        onChange: handleChange,
        fieldNames: fieldNames,
        treeExpandedKeys: treeNodesBySearch.length ? searchExpandedKeys : expandedKeys,
        onTreeExpand: function onTreeExpand(keys) {
          return setExpandedKeys(keys);
        },
      },
      rest,
      searchProps,
      {
        multiple: false,
      },
    ),
  );
});
export default createField(LazyTreeSelect);
